
function soapWrapFunc(bdStr){
    // 封装SOAP头尾
    var soapHead = '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/"><env:Body>', soapTail = '</env:Body></env:Envelope>';
    return soapHead + bdStr + soapTail;
}
//删除考试，通过ID
function deleteMesById(id){
	//验证报文
    var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
    
    var soapBody = '<web:DeleteMesInfo>'+userinfo+'<id>' + id + '</id></web:DeleteMesInfo>';
    $.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/onlineRegisterMessageService',
        data: soapWrapFunc(soapBody),
        dataType: 'xml',
        success: function(xml){
            alert($(xml).find('return').text());
			 loadMesData();
        },
        beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
        	var xmlObj = $(x.responseText);
            if (xmlObj.find('faultstring').text() == 'oops') {
                alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                return;
            }
            else {
                alert('error:' + x.responseText);
            }
        },
        complete: function(x){
        
        }
    });
}
function editMes(id){
	
    
					var getMesStrBody = '<web:GetMesInfoById><id>'+id+'</id></web:GetMesInfoById>';
    
    			$.ajax({
        			type: "POST",
       		 		contentType: "text/xml",
       		 		url: '../../Service/onlineRegisterMessageService',
       		 		data: soapWrapFunc(getMesStrBody),
        			dataType: 'xml',
        			success: function(xml){
						$.each($(xml).find('item'), function(i, n){
							$('#registerNum').val($(this).find('mes_studentId').text());
							$('#title').val($(this).find('mes_title').text());
							$('#date').val(new String($(this).find('mes_submitTime').text()).replace('T', ' ').replace('+08:00', ''));
							$('#content').val($(this).find('mes_content').text());
							$('#managerReply').val($(this).find('mes_reply').text());

            			});
						},
					beforeSend: function(xhr){
            			xhr.setRequestHeader("SOAPAction", "\"\"");
        			},
       	 			error: function(x, e){
       	 			
       	                alert('error:' + x.responseText);
       	            
        			},
        			complete: function(x){
        
        			}
						
					
				});
}
//加载留言数据
function loadMesData(){
	 var getMesStrBody = '<web:GetAllMesInfo></web:GetAllMesInfo>';
    
    $.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/onlineRegisterMessageService',
        data: soapWrapFunc(getMesStrBody),
        dataType: 'xml',
        success: function(xml){
            var tableHtml = '<tbody>';
			var tableHtmlDo = '<tbody>';
            $.each($(xml).find('item'), function(i, n){
				var isChecked=$(this).find('mes_isChecked').text();
				if (isChecked == 0) {
					tableHtml += (i % 2 == 0) ? '<tr>' : '<tr class="odd">';
					tableHtml += '<td>' + (i + 1) + '<input type="hidden" value="' + $(this).find('mes_id').text() + '" /></td>';
					tableHtml += '<td>' + $(this).find('mes_studentId').text() + '</td>';
					tableHtml += '<td>' + i + 200 + '</td>';
					tableHtml += '<td>' + $(this).find('mes_title').text() + '</td>';
					tableHtml += '<td><a href="javascript:void(0);" class="edit" >编辑</a><a href="javascript:void(0);" class="delete" >删除</a></td>';
					tableHtml += '</tr>';
				}
				else if (isChecked == 1) {
					tableHtmlDo += (i % 2 == 0) ? '<tr>' : '<tr class="odd">';
					tableHtmlDo += '<td>' + (i + 1) + '<input type="hidden" value="' + $(this).find('mes_id').text() + '" /></td>';
					tableHtmlDo += '<td>' + $(this).find('mes_studentId').text() + '</td>';
					tableHtmlDo += '<td>' + i + 200 + '</td>';
					tableHtmlDo += '<td>' + $(this).find('mes_title').text() + '</td>';
					tableHtmlDo += '<td><a href="javascript:void(0);" class="edit" >编辑</a><a href="javascript:void(0);" class="delete" >删除</a></td>';
					tableHtmlDo += '</tr>';
				}
            });
            tableHtml += '</tbody>';
			tableHtmlDo += '</tbody>';
            $('#mesInfoTable').find('tbody').remove().end().append(tableHtml);
			$('#mesInfoTableDo').find('tbody').remove().end().append(tableHtmlDo);
            //编辑按钮
            $('#mesInfoTable .edit,#mesInfoTableDo .edit').click(function(){

				
                $('#examWnd').show();
                $('#wndTitle span').text('审核留言');
                // 是编辑考试
                $.editMode = 1;
				$.editID = $(this).parents('tr').find('td').eq(0).find(':hidden').val();
				editMes($.editID);	
				
				
				
            });
            //删除按钮
            $('#mesInfoTable .delete,#mesInfoTableDo .delete').click(function(){
                $.editID = $(this).parents('tr').find('td').eq(0).find(':hidden').val();
                deleteMesById($.editID);
            });
        },
        beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
            alert('error:' + x.responseText);
        },
        complete: function(x){
        
        }
    });
}

$(function(){
	$.editMode=0;
    
});
/*
 *
 *
 */
$(window).load(function(){
	loadMesData();
	//拖拽绑定
	$('#examWnd').dragDrop({ focuEle: '#wndTitle' });
				
    $('.timezone').datePicker({
        startDate: '2000-01-01',
        endDate: '2100-01-01',
        clickInput: true,
        verticalOffset: 35
    });
    $('#closeBtn').click(function(){
        $('#maskLayer').hide();
        $('#examWnd').find('input[type="text"]').val('').end().hide();
    });
    // 新增考试
    $('#newButton').click(function(){
        $('#maskLayer').show();
        $('#examWnd').show();
        $('#wndTitle span').text('新增考试信息');
        // 不是编辑考试，是新增考试
        $.editMode = 0;
    });
    // 文本框获得焦点的时候，显示TIP
    $('#examWnd input[type="text"]').focusin(function(){
        $(this).parent().prev('.tip').show();
    }).focusout(function(){
        $('#examWnd .tip').hide();
    });
    $('#saveButton').click(function(){
    	//验证报文
        var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
    	
        var registerNum = $.trim($('#registerNum').val()), title = $.trim($('#title').val()), Content = $.trim($('#content').val()), managerReply = $.trim($('#managerReply').val());
        var date = $.trim(new String($('#date').val()).replace(' ','T'));
		/*
         * 这里要进行输入验证
         * 由大家完成
         * 正则或其他验证等等
         * 
         * 
         * 
         * 
         * 
         * 
         */
        var idValue, soapFuncName;
        //新增考试
        if ($.editMode == 0) {
            soapFuncName = 'DeleteMesInfo';
            idValue = '';
        }
        else {
            //审核留言，需要多发送一个留言ID信息
            soapFuncName = 'ModifyMesInfo';
            idValue = $.editID;
        }
        var sendBody = '<web:' + soapFuncName + '>'+userinfo+'<mesEntry>' +
        '<mes_id>' +
        idValue +
        '</mes_id>' +
        '<mes_studentId>' +
        registerNum +
        '</mes_studentId>'+
		'<mes_title>' +
        title +
        '</mes_title>' +
        '<mes_content>' +
        Content +
        '</mes_content>' +
        '<mes_submitTime>' +
        date +
        '</mes_submitTime>' +
        '<mes_reply>' +
		managerReply			+
        '</mes_reply>' +
        '<mes_isChecked>' +
        $.editMode +
        '</mes_isChecked>' +
        '</mesEntry></web:' +
        soapFuncName +
        '>';
        $.ajax({
            type: "POST",
            contentType: "text/xml",
            url: '../../Service/onlineRegisterMessageService',
            data: soapWrapFunc(sendBody),
            dataType: 'xml',
            success: function(xml){
                alert($(xml).find('return').text());
				 loadMesData();
            },
            beforeSend: function(xhr){
                xhr.setRequestHeader("SOAPAction", "\"\"");
            },
            error: function(x, e){
            	var xmlObj = $(x.responseText);
                if (xmlObj.find('faultstring').text() == 'oops') {
                    alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                    return;
                }
                else {
                    alert('error:' + x.responseText);
                }
            },
            complete: function(x){
            
            }
        });
    });
});
